<script setup lang="ts">
import APIUploadMaterial from '@/api/upload-material'
import APICommon from '@/api/common'
import type { Props, Emits } from './utils/types'
import { formatBytes } from '@/utils/index'
import * as qiniu from 'qiniu-js'

const $props = defineProps<Props>()
const $emit = defineEmits<Emits>()

async function getFileToken() {
    const form = {
        copyright_image: $props.formFile.copyright_image,
        main_image: $props.formFile.main_image,
        score: `${$props.formFile.score_1},${$props.formFile.score_2},${$props.formFile.score_3}`
    }
    const { data } = await APIUploadMaterial.fontToken(form)
    return data.token
}

const progress = shallowRef<number>(0)

const isSuccee = shallowRef<boolean>(false)

const token = shallowRef<string>('')
const subscription = shallowRef<any>()
onUnmounted(() => {
    subscription.value?.unsubscribe()
})

async function handleUpload() {
    token.value = await getFileToken()
    const observable = qiniu.upload($props.file, '', token.value)
    subscription.value = observable.subscribe({
        next(res: any) {
            progress.value = Math.floor(res?.total?.percent)
        },
        error() {
            progress.value = 0
        },
        complete(res: any) {
            progress.value = 100
            isSuccee.value = true
            $emit('on-success', res.data.code)
        }
    })
}

function handleRemove() {
    $emit('on-remove')
}

/** 普通图片上传 */
function handleNormalUpload() {
    if ($props.file) {
        const form = new FormData()
        form.append('file', $props.file)
        APICommon.uploadFile(form).then(({ data }) => {
            progress.value = 100
            isSuccee.value = true
            $emit('on-normal-upload', data.file)
        })
    }
}

defineExpose({
    handleUpload,
    handleNormalUpload
})



</script>

<template>
    <div class="upload-progress">
        <div class="text" v-if="file">
            <div class="left">
                <div>{{ file?.name }}</div>
                <div v-if="isSuccee">上传完成</div>
            </div>
            <div class="right">
                <div class="delete" v-if="!token" @click="handleRemove">删除</div>
                <div class="bytes">{{ formatBytes(file?.size || 0) }}</div>
            </div>
        </div>
        <div class="progress" :style="`width: ${progress}%`"></div>
    </div>
</template>

<style lang="scss">
.upload-progress {
    height: 45px;
    padding: 0 28px;
    padding-top: 10px;
    box-sizing: border-box;


    &:hover {
        background-color: #ECF5FE;
    }

    .progress {
        height: 8px;
        width: 0;
        background-color: #F91414;
        margin-top: 5px;
    }

    .text {
        display: flex;
        justify-content: space-between;
        font-size: 13px;

        .right {
            .delete {
                color: rgba(55, 133, 242, 1);
                cursor: pointer;

                &:hover {
                    text-decoration: underline;
                }
            }

            .bytes {
                min-width: 80px;
                text-align: right;
            }
        }

        .left,
        .right {
            display: flex;
            gap: 0 30px;
        }
    }

}
</style>
